package com.design_mode;

import java.util.Iterator;
import java.util.Vector;

/**
 * Created by dustaple on 8/1/14.
 */
public class IteratorPattern {

    public static void main(String[] args) {
        Aggregate aggregate = new StudentAggregate();
        Student stu1 = new Student();
        Student stu2 = new Student();
        Student stu3 = new Student();
        aggregate.add(stu1);
        aggregate.add(stu2);
        aggregate.add(stu3);
        Iterator iterator = aggregate.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

class Student {
    String name;
    Boolean gender;
    Integer age;
    String studentCode;
    Integer score;
}

interface Aggregate {
    void add(Object object);

    Iterator iterator();
}

class StudentIterator implements Iterator {
    private Vector<Student> vectors;
    private int cursor;

    public StudentIterator(Vector<Student> vct) {
        this.vectors = vct;
        cursor = 0;
    }

    @Override
    public boolean hasNext() {

        return cursor != vectors.size();
    }

    @Override
    public Object next() {
        Object object = null;
        if (cursor != vectors.size()) {
            object = vectors.get(cursor);
            ++cursor;
        }
        return object;
    }

    @Override
    public void remove() {
        if (cursor + 1 < vectors.size()) {
            vectors.remove(cursor);
        }
    }
}


class StudentAggregate implements Aggregate {
    private Vector<Student> students = new Vector<Student>();

    @Override
    public void add(Object object) {
        students.add((Student) object);
    }

    @Override
    public Iterator iterator() {
        return new StudentIterator(students);
    }
}
